﻿using System.Linq;
using System.Xml.Linq;
using OpenXmlPowerTools;

namespace Songhay.OpenXml
{
    /// <summary>
    /// Static members for Open XML.
    /// </summary>
    public static partial class OpenXmlUtility
    {
        /// <summary>
        /// Annotates the content of the hyperlink.
        /// </summary>
        /// <param name="rootElement">The root element.</param>
        /// <remarks>
        /// This method is written by Eric White.
        /// 
        /// For more detail, see:
        /// 
        /// “Using Annotations to Transform LINQ to XML Trees in an XSLT Style (Improved Approach)”
        /// http://blogs.msdn.com/b/ericwhite/archive/2008/06/23/using-annotations-to-transform-linq-to-xml-trees-in-an-xslt-style-improved-approach.aspx
        /// 
        /// </remarks>
        public static void AnnotateHyperlinkContent(XElement rootElement)
        {
            AnnotateState state = AnnotateState.NotInHyperlink;
            foreach(XElement blockLevelContentContainer in
                rootElement.Descendants().Where(e => SonghayW.BlockLevelContentContainers.Contains(e.Name)))
            {
                FieldInfo fieldInfo = null;
                foreach(XElement runLevelContent in blockLevelContentContainer
                    .LogicalChildrenContent(SonghayW.p).LogicalChildrenContent(SonghayW.r))
                {
                    if(runLevelContent.Elements(SonghayW.fldChar).Attributes(SonghayW.fldCharType)
                        .Any(a => a.Value == "begin"))
                        state = AnnotateState.InFirstSection;
                    XElement instrText = runLevelContent.Elements(SonghayW.instrText).FirstOrDefault();
                    if(instrText != null && state == AnnotateState.InFirstSection)
                    {
                        FieldInfo tempFieldInfo = FieldParser.ParseField(instrText.Value);
                        if(tempFieldInfo.FieldType == "HYPERLINK")
                            fieldInfo = tempFieldInfo;
                    }
                    var z = runLevelContent.Elements(SonghayW.fldChar).FirstOrDefault();
                    if(runLevelContent.Elements(SonghayW.fldChar).Attributes(SonghayW.fldCharType)
                        .Any(a => a.Value == "separate"))
                        state = AnnotateState.InSecondSection;
                    if(runLevelContent.Elements(SonghayW.fldChar).Attributes(SonghayW.fldCharType)
                        .Any(a => a.Value == "end"))
                    {
                        fieldInfo = null;
                        state = AnnotateState.NotInHyperlink;
                    }
                    if(state == AnnotateState.InSecondSection && fieldInfo != null &&
                        (string)runLevelContent.Elements(SonghayW.rPr).Elements(SonghayW.rStyle)
                        .Attributes(SonghayW.val).FirstOrDefault() == "Hyperlink")
                        runLevelContent.AddAnnotation(fieldInfo);
                }
            }
        }
    }
}
